<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Email;
use think\Db;
use think\Env;

/**
 * 询盘
 *
 * @icon fa fa-circle-o
 */
class Message extends Api
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    /**
     * 模型对象
     * @var \app\common\model\Message
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Message;
    }


    /**
     * 新增
     *
     */
    public function add()
    {
        $param = $this->request->post(false);
        $param['status'] = 'normal';
        $ret = $this->model->save($param);
        $content = sprintf($this->moban(),$param['name'],$param['email'],$param['company'],$param['job_title'],
            $param['business_address'],$param['phone'],$param['coil'],$param['quantity'],$param['material'],$param['requirements']);
        //发送邮件
        $obj = new Email();
        $result = $obj
            ->to(Env::get("app.toemail"))
            ->subject('询盘表单提交')
            ->message($content)
            ->send();
//        var_dump($result);
        if ($ret) {
            $this->success('success');
        } else {
            $this->error($this->model->getError());
        }
    }

    private function moban(){
        return '<table width="600" style="border:0">
  <tbody>
      <tr class="firstRow">
          <td style="padding:0 0 0 0">
              <p style="margin-top:48px;margin-right:0;margin-bottom:   48px;margin-left:0;line-height:140%">
                  <span style="font-size: 32px;"><strong>收到询盘邮件</strong></span>
              </p>
          </td>
      </tr>
      <tr>
          <td style="padding:0 0 0 0">
              <p style="margin-bottom:16px;line-height:140%">
                  <strong>客户姓名：</strong> %s
              </p>
              <p style="margin-bottom:16px;line-height:140%">
                  <strong>公司邮件：</strong> %s
              </p>
              <p style="margin-bottom:16px;line-height:140%">
                  <strong>公司名称：</strong> %s
              </p>
              <p style="margin-bottom:16px;line-height:140%">
                  <strong>工作职位：</strong> %s
              </p>
              <p style="margin-bottom:16px;line-height:140%">
                  <strong>公司地址：</strong> %s
              </p>
              <p style="margin-bottom:16px;line-height:140%">
                  <strong>联系电话：</strong> %s
              </p>
              <p style="margin-bottom:16px;line-height:140%">
                  <strong>产品类型：</strong> %s
              </p>
              <p style="margin-bottom:16px;line-height:140%">
                  <strong>产品数量：</strong> %s
              </p>
              <p style="margin-bottom:16px;line-height:140%">
                  <strong>产品材料：</strong> %s
              </p>
              <p style="margin-bottom:16px;line-height:140%">
                  <strong>包装要求：</strong> %s
              </p>
          </td>
      </tr>
  </tbody>
</table>';
    }
}
